/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gr.anagnosg.general.utils;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Collection;
import java.util.HashMap;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.NameFileFilter;

/**
 *
 * @author anagnosg
 */
public class ImageUtils {
    
    
    public static HashMap  splitMultiPageTiff(String inputFilename,String outputFilename,boolean deletebefore){
                HashMap hm = new HashMap();
                File f = new File(inputFilename);
                String parent= f.getParent();
                File parentDir = new File(parent);
                
                String fileOutputStr = outputFilename.substring(outputFilename.lastIndexOf("/")+1,outputFilename.indexOf("%d"));
                
                FileNameContainsFilter filter = new FileNameContainsFilter(fileOutputStr);
                File[] before = parentDir.listFiles(filter);
                int beforePages = 0;
                if(deletebefore){
                    FileUtils.deleteFiles(before );
                }
                else{
                    beforePages = before.length;
                }
                
                String commandSplitMultiPageTiff="convert "+inputFilename+" " + outputFilename;
                String output = ExtrernalUtils.executeCommand(commandSplitMultiPageTiff);
                
                File[] after = parentDir.listFiles(filter);
                int pages =after.length -  beforePages;
                //System.out.print("pages"+pages);
                File[] orderAfter = FileUtils.orderByPage(after);
                hm.put("pages", pages);
                hm.put("files", orderAfter);
                return hm;
    
    }
    
    
    public static HashMap convert2pgm(File[] files){
        /*HashMap hm = new HashMap();
        File fout[] = new File[files.length];
        for(int i= 0 ; i < files.length; i++){
            File ft = files[i];
            String pathPrefix=ft.getParent()+File.separatorChar;
            String fn =FileUtils.getFilenameNoExt(ft);
            String commandConvertPmg = " convert "+pathPrefix+ft.getName()+" "+pathPrefix+fn+".pgm ";
            System.out.println(commandConvertPmg);
            String output = ExtrernalUtils.executeCommand(commandConvertPmg);
            File fnT = new File(ft.getParent()+File.separatorChar+fn+".pgm");
            fout[i]=fnT;
        }
        hm.put("files", fout);
        return hm;
        */
        return convertImage(files,"pgm");
    }
    
    public static HashMap convertImage(File[] files,String toExt){
        HashMap hm = new HashMap();
        File fout[] = new File[files.length];
        for(int i= 0 ; i < files.length; i++){
            File ft = files[i];
            String pathPrefix=ft.getParent()+File.separatorChar;
            String fn =FileUtils.getFilenameNoExt(ft);
            String commandConvertPmg = " convert "+pathPrefix+ft.getName()+" "+pathPrefix+fn+"."+toExt;
            System.out.println(commandConvertPmg);
            String output = ExtrernalUtils.executeCommand(commandConvertPmg);
            File fnT = new File(ft.getParent()+File.separatorChar+fn+"."+toExt);
            fout[i]=fnT;
        }
        hm.put("files", fout);
        return hm;
    
    }
    
    public static HashMap unpaperPages(File files[]){
    
        HashMap hm = new HashMap();
        File fnArray[] = new File[files.length];
        for(int i= 0 ; i < files.length; i++){
            File ft = files[i];
            String pathPrefix=ft.getParent()+File.separatorChar;
            String fn =FileUtils.getFilenameNoExt(ft);
            String commandConvertPmg = " unpaper "+pathPrefix+ft.getName()+" "+pathPrefix+fn+"unp.pgm";
            System.out.println(commandConvertPmg);
            String output = ExtrernalUtils.executeCommand(commandConvertPmg);
            File filn = new File(pathPrefix+fn+"unp.pgm");
            fnArray[i]=filn;
        }
        hm.put("files", fnArray);
        return hm;
        
    }
    
    
    
    
    public static void main (String args[]){
        String inputFilename = "/home/anagnosg/Documents/ocr/train14/scanAll.tif";
        String outputFilename = "/home/anagnosg/Documents/ocr/train14/Sscan%d.tif ";
        HashMap data = ImageUtils.splitMultiPageTiff( inputFilename, outputFilename,true);
        File f[] = (File[])data.get("files");
        HashMap data2 = ImageUtils.convert2pgm(f);
        File  f2[]=(File[])data2.get("files");
        HashMap data3= unpaperPages(f2);
        File  f3[]=(File[])data3.get("files");
        HashMap data4 = convertImage(f3,"tif");
        File  f4[]=(File[])data4.get("files");
        HashMap data5 = Ocr.recognize(f4," -l ell");
    }
            
            
}
